#include"Protocol.hpp"

enum 
{
    DivErr = 1,
    ModErr,
    OtherErr,
};

class Calculator
{
public:
    Calculator()
    {}

    ~Calculator()
    {}

    Response CalculatorHelper(const Request& req)
    {
        Response resp(0,0);
        switch(req.op)
        {
        case '+':
            resp.result = req.x + req.y;
            break;
        case '-':
            resp.result = req.x - req.y;
            break;
        case '*':
            resp.result = req.x * req.y;
            break;
        case '/':
        {
            if(req.y == 0)  resp.code = DivErr;
            else    resp.result = req.x / req.y;  
        }
        break;
        case '%':
        {
            if(req.y == 0)  resp.code = ModErr;
            else    resp.result = req.x % req.y;  
        }   
        break;
        default:
            resp.code = OtherErr;
            break;
        }

        return resp;
    }

    std::string Handle(std::string& package) 
    {
        std::string content;
        bool r = Decode(package,&content); //"len"\n"10 + 20"\n
        if(!r)  return "";
        Request req;
        r = req.DeSerialize(content); //"10 + 20"\n
        if(!r)  return "";

        content = "";
        Response resp = CalculatorHelper(req);
        resp.Serialize(&content);
        content = Encode(content);

        return content;
    }
};